// 定义组件
define(function (require,exports,module) {
    var VueStore = require("charity/js/store/index")
        ,store = new Vuex.Store(VueStore);
    var rem=require('charity/js/config/rem'),//rem字体
        list = {
            // 组件
            components : {
                'v-loading':require('charity/js/components/loading.js')  // 加载中组件
            },
            data : {
                positionY:'',
                banner:global.cxt+'/view/charity/images/list/banner.jpg',
                searchIpt:'',//搜索输入
                resultShow:false,//模糊搜索列表是否显示
                resultItem:'',//选中的模糊搜索
                result:[],//模糊搜索塞入
                action:{},
                lists:[],
                loading:false,
                over:false,//是否没有更多数据
                searchAfter:false,
                page:0,//当前页
                listUrl:global.cxt+'/charity/goods/list',
                searchUrl:global.cxt+'/charity/goods/list',//搜索
                vagueSearchUrl:global.cxt+'/charity/goods/names',//模糊搜索
                showLoading:true//加载中
            },
            // 事件
            methods : {
                // 下拉加载更多
                loadMore:function () {
                    var that=this;
                    // 正在loading或查询过后
                    if(that.loading||that.searchAfter){
                        return;
                    }
                    that.loading = true;//loading
                    that.page+=1;//翻页
                    // 请求
                    $.ajax({
                        url:that.listUrl,
                        type:'post',
                        data:{'page_number':that.page},
                        dataType:'json',
                        success:function (res) {
                            // 关闭loading
                            that.loading = false;
                            // 成功
                            if(res.code=='111111') {
                        		window.location.href = res.data;
                        		return;
                        	}
                            if(res.code=='0'){
                                for (var i = 0; i <res.data.list.length; i++) {
                                	res.data.list[i].image = global.cxt+'/view/charity/images/temp/' + res.data.list[i].image;
                                    that.lists.push(res.data.list[i]);
                                }
                                // 当数据小于10条时，loading长显示，显示数据为空
                                if(res.data.list.length<20){
                                    that.loading=true;
                                    that.over=true;
                                    that.$messagebox({title:'温馨提示',message:'没有更多数据了~',showCancelButton: true})
                                }
                            }else{
                            	that.$messagebox({title:'温馨提示',message:res.msg,showCancelButton: true})
                            }
                        }
                    })
                },
                // 搜索
                search:function () {
                    var that=this;
                    that.resultShow=false;//关闭模糊搜索
                    that.loading=true;
                    that.over=true;
                    /*if(that.searchIpt.length==''){
                        that.$messagebox({title:'温馨提示',message:'请输入搜索内容！',showCancelButton: true})
                        return;
                    }*/
                    // 显示loading
                    that.$indicator.open({
                        spinnerType: 'fading-circle'
                    });
                    // 请求
                    $.ajax({
                        url:that.searchUrl,
                        type:'post',
                        dataType:'json',
                        data: {name: that.searchIpt, page_number:1, page_size: 99999999},
                        success:function (res) {
                            // 关闭loading
                            that.$indicator.close();
                            // 登录成功
                            if(res.code=='0'){
                                //that.searchAfter=true;//停止下拉加载
                                for (var i = 0; i <res.data.list.length; i++) {
                                	res.data.list[i].image = global.cxt+'/view/charity/images/temp/' + res.data.list[i].image;
                                }
                                that.lists=res.data.list;
                            }else{
                                that.$messagebox({title:'温馨提示',message:'没有找到你要的商品，再找个别的试试吧',showCancelButton: true})
                            }
                        }
                    })
                },
                // 模糊查询
                vagueSearch:function () {
                    var that=this,
                        v=event.target.value;
                    // 请求
                    $.ajax({
                        url:that.vagueSearchUrl,
                        type:'post',
                        data:{name:v},
                        dataType:'json',
                        success:function (res) {
                            // 关闭loading
                            that.$indicator.close();
                            // 登录成功
                            if(res.code=='0'){
                            	var names = [];
                            	$(res.data).each(function(i, e) {
                            		names.push(e.name);
                            	});
                                that.result=names;
                            }else{
                                // 错误提示
                            }
                        }
                    })
                },
                loadAction: function() {
                	var that=this;
                	//活动信息
                    $.ajax({
                        url:global.cxt+'/charity/action/get',
                        type:'post',
                        dataType:'json',
                        success:function (res) {
                            // 关闭loading
                        	that.$indicator.close();
                        	if(res.code=='111111') {
                        		window.location.href = res.data;
                        		return;
                        	}
                            
                            if(res.code=='0'){
                            	res.data.start_time = res.data.start_time.parseDate();
                            	res.data.end_time = res.data.end_time.parseDate();
                            	res.data.detial = res.data.detial.replaceAll('\n', '<br>');
                            	var d = new Date();
                            	//为开始
                            	if(d.getTime()<res.data.start_time.getTime())
                            		res.data.state = 'stateWait';
                            	//进行中
                            	if(d.getTime()>=res.data.start_time.getTime() && d.getTime()<=res.data.end_time.getTime())
                            		res.data.state = 'stateBe';
                            	//已结束
                            	if(d.getTime() > res.data.end_time.getTime())
                            		res.data.state = 'stateStop';
                            	that.action=res.data;
                            }else{
                            	that.$messagebox({title:'温馨提示',message:res.msg,showCancelButton: true})
                            }
                        }
                    })
                }
            }
        };
    
	var html = require('charity/templates/list.tpl'),
	    index = Vue.extend({
	      	template:html,
		    data:function(){
				return list.data
		    },
		    props:[],
	        components:list.components,
	        beforeCreate:function(){
	        	require('charity/css/list.css');
                require('charity/css/components/e-ui-css.css');
                require('charity/js/lib/mint-ui');
	        },
            beforeRouteLeave(to, from, next){
                var  position = document.documentElement.scrollTop || document.body.scrollTop;
                store.commit('SAVE_POSITION', position) //离开路由时把位置存起来
                this.positionY=position;
                next()
            },
		    // 实例被创建之后被调用
		    created:function(){
		    	this.loadAction();
		    },
			computed:{},
            mounted:function(){
                this.showLoading = false;
                window.scrollTo(0, this.positionY)
            },
            watch: {
                searchIpt:function (val) {
                    if(val && val!=this.resultItem){
                        this.resultShow=true;

                    }else{
                        this.resultShow=false;
                        console.log(2)
                    }

                }
            },
		    methods:list.methods
		});
	module.exports = index;

});

